<?php

namespace config;


class parser {
    
    public static $_extensions = array('ini');
    
    public static function ini($file)
    {
        $file = __DIR__ . DIRECTORY_SEPARATOR . $file.'.ini';
        
        if(file_exists($file))
        {
            ob_start();
                include("{$file}");
                $string = ob_get_contents();
            ob_end_clean();

            $string = parse_ini_string($string);
            
            $config = array();
            
            foreach($string as $key=>$val) {
                $config = self::createConfigFromIni($config,$key,$val);
            }
        }
        else 
        {
            throw new Exception('Nie ma takiego pliku.');
        }
        
        return $config;
    }
    
    public static function createConfigFromIni($config, $key, $value) 
    {
        
            if(strpos($key, '.'))
            {
              $newArray = explode('.',$key,2);
              
               if(empty($config[$newArray[0]]))
                {
                    $config[$newArray[0]] = array();
                }
              
              $config[$newArray[0]] = self::createConfigFromIni($config[$newArray[0]],$newArray[1], $value);
            
            }
           
            else
            {
                $config[$key] = $value;
            }
        
        return $config;
    }
    
    public static function getArray() 
    {
        return self::$_extensions;
    }
}

?>
